package model;

import java.util.ArrayList;

import common.DataManager;
import common.GridCoordinate;
import controller.Controller;

/**
 * @author Colin
 * 
 * The SpikeTrap is a defensive item that attacks zombosaurs when they
 * are on the same tile as the SpikeTrap. SpikeTraps can attack 10 
 * zombosaurs before being removed.
 *
 */
public class SpikeTrap extends DefensiveItem {
	
	private static final int INITIAL_HEALTH = 10;
	private static final int INITIAL_DAMAGE = 1; //Maybe change to 0.5
	private static final int COST = 75;
	
	private int damage = INITIAL_DAMAGE;
	private boolean destroyed = false;
	private int remainingHealth = INITIAL_HEALTH;	//one health taken away every zombosaur that passes
	private ArrayList<Zombosaur> hurtZombosaurs = new ArrayList<Zombosaur>();
	
	//SpikeTrap without position
	public SpikeTrap(){
		super("Spike Trap", DataManager.SPIKE_TRAP, COST, INITIAL_HEALTH, INITIAL_DAMAGE);
		
	}
	
	//SpikeTrap with position
	public SpikeTrap(GridCoordinate position) {
		super("Spike Trap", DataManager.SPIKE_TRAP, COST, position, INITIAL_HEALTH, INITIAL_DAMAGE);
		remainingHealth = INITIAL_HEALTH;
	}

	
	/**
	 * Health is decreased by one
	 */
	public void onContact( Zombosaur zombosaur){
		if(zombosaur instanceof Pzombiedactyl) return;
		boolean zomboAlreadyOnTile = false;
		for(int i = 0; i < hurtZombosaurs.size(); i++){
			if(hurtZombosaurs.get(i) == zombosaur) zomboAlreadyOnTile = true;
		}
		if(!zomboAlreadyOnTile) {
			hurtZombosaurs.add(zombosaur);
			float vectorY = -(zombosaur.getyPos()-(this.getPosition().getY()+0.5f));
			float vectorX = (zombosaur.getxPos()-(this.getPosition().getX()+0.5f));
			float angle = (float)Math.atan(vectorY/vectorX);
			if(vectorX < 0) angle += Math.PI;	//atan returns an angle between -PI/2 and PI/2. This corrects for that.
			zombosaur.hit(damage,(int)(angle*180/Math.PI));
		}
		System.out.println(hurtZombosaurs.size());
		if(hurtZombosaurs.size() >= 10){
			destroyed();
		}
	}
	
	private void destroyed(){
		this.destroyed = true;
	}
	
	public boolean isDestroyed(){
		return destroyed;
	}
	
	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}

	@Override
	public String getDescription() {
		return "The Spike Trap deals one damage to the first ten zombosaurs to pass over it.";
	}
	
}
